﻿import pandas as pd

# 显示全体数据
pd.set_option('display.max_columns', None)


# 读取csv,excel
def csv_read(path, clas, Wire_diameter, chip_size, DB_glue):
    df_pre = pd.DataFrame()
    df_after = pd.DataFrame()
    df_after1 = pd.DataFrame()
    df_after2 = pd.DataFrame()
    df_after3 = pd.DataFrame()
    df_after4 = pd.DataFrame()
    df_after5 = pd.DataFrame()
    list_df = []
    list_df1 = []
    list_df2 = []
    list_df3 = []
    list_df4 = []
    list_df5 = []

    row_num = 0
    mark = 1
    mark1 = 1
    mark2 = 1
    mark3 = 1
    mark4 = 1
    mark5 = 1

    # 计算是否有空行
    f = open(path, encoding='gbk')
    str1 = f.readline()
    f.close()
    start_num = 0 if str1.startswith('设备型号') else 1

    df1 = pd.read_csv(path, encoding='gbk', header=None, usecols=[0, 1, 2])
    index1 = df1[df1[0] == 'Cpk, Cp'].index.values[0]

    df_on = df1.loc[1:index1, :3]
    sql_cols = ['TestDate', 'TestTime', 'TestNo', 'Operator', 'Class', 'MachineID', 'WireDiameter', 'ChipSize',
                'DB_glue',
                'Operator', 'LoadUnit', 'Sensor', 'TestMethod', 'AcceptanceVlaue', 'TestingSpeed', 'ShearHeight']

    # 将上半部分的信息转换威df
    for i in range(len(sql_cols)):
        df_pre.loc[0, sql_cols[i]] = df_on.iloc[i, 1]

    # 读取测试的值，并对列进行命名
    df_down = pd.read_csv(path, encoding='gbk', header=None, usecols=[0, 1, 2, 3, 4], skiprows=index1 + start_num + 1)
    df_down = df_down.reset_index(drop=True)

    number_flag, string_des = get_file_type(clas, df_down, start_num, index1)

    if number_flag < 0:
        list_df = string_des
        return list_df, number_flag, string_des
    elif number_flag > 0:
        list_df = f'{clas}文件,{number_flag}行应为{string_des}'
        return list_df, number_flag, string_des
    else:
        if string_des == '固晶推力':
            chip_size_g = df_pre.loc[0, 'ChipSize']
            DB_glue_g = df_pre.loc[0, 'DB_glue']
            if (not chip_size or chip_size_g == chip_size) and (not DB_glue or str(DB_glue_g) in str(DB_glue)):
                for i in range(len(df_down)):
                    df_after.loc[0, f'{"DATA" + str(mark)}'] = df_down.loc[i, 3]
                    if mark == 60 or i == len(df_down) - 1:
                        list_df.append(pd.concat([df_pre, df_after], axis=1))
                        df_after = pd.DataFrame()
                        mark = 0
                    mark += 1
                return [list_df], 0, string_des
            else:
                if chip_size and chip_size_g != chip_size:
                    return '测量时，设置的芯片尺寸与工单不一致！', None, None
                elif DB_glue and DB_glue_g not in DB_glue:
                    return '测量时，设置的固晶胶与工单不一致！', None, None
                else:
                    return '测量时，未设置的芯片尺寸与固晶胶！', None, None

        elif string_des == '焊线推力':
            Wire_diameter_hx = df_pre.loc[0, 'WireDiameter']
            chip_size_hx = df_pre.loc[0, 'ChipSize']
            if (not chip_size or chip_size_hx == chip_size) and (
                    not Wire_diameter or Wire_diameter_hx in Wire_diameter):
                for i in range(len(df_down)):
                    j = i % 6
                    if j == 0 or j == 1:
                        df_after1.loc[0, f'{"DATA" + str(mark1)}'] = df_down.loc[i, 3]
                        if mark1 == 60 or i == len(df_down) - 5:
                            list_df1.append(pd.concat([df_pre, df_after1], axis=1))
                            df_after1 = pd.DataFrame()
                            mark1 = 0
                        mark1 += 1
                    elif j == 2 or j == 3:
                        df_after2.loc[0, f'{"DATA" + str(mark2)}'] = df_down.loc[i, 3]
                        if mark2 == 60 or i == len(df_down) - 3:
                            list_df2.append(pd.concat([df_pre, df_after2], axis=1))
                            df_after2 = pd.DataFrame()
                            mark2 = 0
                        mark2 += 1
                    elif j == 4 or j == 5:
                        df_after3.loc[0, f'{"DATA" + str(mark3)}'] = df_down.loc[i, 3]
                        if mark3 == 60 or i == len(df_down) - 1:
                            list_df3.append(pd.concat([df_pre, df_after3], axis=1))
                            df_after3 = pd.DataFrame()
                            mark3 = 0
                        mark3 += 1
                return [list_df1, list_df2, list_df3], 0, string_des
            else:
                if chip_size and chip_size_hx != chip_size:
                    return '测量时，设置的芯片尺寸与工单不一致！', None, None
                elif Wire_diameter and Wire_diameter_hx not in Wire_diameter:
                    return '测量时，设置的金线线径与工单不一致！', None, None
                else:
                    return '测量时，未设置的金线线径与芯片尺寸！', None, None

        elif string_des == '焊线推力-齐纳':
            Wire_diameter_hx = df_pre.loc[0, 'WireDiameter']
            chip_size_hx = df_pre.loc[0, 'ChipSize']
            if (not chip_size or chip_size_hx == chip_size) and (
                    not Wire_diameter or Wire_diameter_hx in Wire_diameter):
                for i in range(len(df_down)):
                    j = i % 10
                    if j == 0 or j == 1:
                        df_after1.loc[0, f'{"DATA" + str(mark1)}'] = df_down.loc[i, 3]
                        if mark1 == 60 or i == len(df_down) - 9:
                            list_df1.append(pd.concat([df_pre, df_after1], axis=1))
                            df_after1 = pd.DataFrame()
                            mark1 = 0
                        mark1 += 1
                    elif j == 2 or j == 3:
                        df_after2.loc[0, f'{"DATA" + str(mark2)}'] = df_down.loc[i, 3]
                        if mark2 == 60 or i == len(df_down) - 7:
                            list_df2.append(pd.concat([df_pre, df_after2], axis=1))
                            df_after2 = pd.DataFrame()
                            mark2 = 0
                        mark2 += 1
                    elif j == 4 or j == 5:
                        df_after3.loc[0, f'{"DATA" + str(mark3)}'] = df_down.loc[i, 3]
                        if mark3 == 60 or i == len(df_down) - 5:
                            list_df3.append(pd.concat([df_pre, df_after3], axis=1))
                            df_after3 = pd.DataFrame()
                            mark3 = 0
                        mark3 += 1
                    elif j == 6 or j == 7:
                        df_after4.loc[0, f'{"DATA" + str(mark4)}'] = df_down.loc[i, 3]
                        if mark4 == 60 or i == len(df_down) - 3:
                            list_df4.append(pd.concat([df_pre, df_after4], axis=1))
                            df_after4 = pd.DataFrame()
                            mark4 = 0
                        mark4 += 1
                    elif j == 8 or j == 9:
                        df_after5.loc[0, f'{"DATA" + str(mark5)}'] = df_down.loc[i, 3]
                        if mark5 == 60 or i == len(df_down) - 1:
                            list_df5.append(pd.concat([df_pre, df_after5], axis=1))
                            df_after5 = pd.DataFrame()
                            mark5 = 0
                        mark5 += 1
                return [list_df1, list_df2, list_df3, list_df4, list_df5], 0, string_des
            else:
                if chip_size and chip_size_hx != chip_size:
                    return '测量时，设置的芯片尺寸与工单不一致！', None, None
                elif Wire_diameter and Wire_diameter_hx not in Wire_diameter:
                    return '测量时，设置的金线线径与工单不一致！', None, None
                else:
                    return '测量时，未设置的金线线径与芯片尺寸！', None, None

        elif string_des == '焊线推力-倒装':
            Wire_diameter_hx = df_pre.loc[0, 'WireDiameter']
            chip_size_hx = df_pre.loc[0, 'ChipSize']
            if (not chip_size or chip_size_hx == chip_size) and (
                    not Wire_diameter or Wire_diameter_hx in Wire_diameter):
                for i in range(len(df_down)):
                    j = i % 10
                    if j <= 4:
                        df_after1.loc[0, f'{"DATA" + str(mark1)}'] = df_down.loc[i, 3]
                        if mark1 == 60 or i == len(df_down) - 6:
                            list_df1.append(pd.concat([df_pre, df_after1], axis=1))
                            df_after1 = pd.DataFrame()
                            mark1 = 0
                        mark1 += 1
                    elif j >= 5:
                        df_after2.loc[0, f'{"DATA" + str(mark2)}'] = df_down.loc[i, 3]
                        if mark2 == 60 or i == len(df_down) - 1:
                            list_df2.append(pd.concat([df_pre, df_after2], axis=1))
                            df_after2 = pd.DataFrame()
                            mark2 = 0
                        mark2 += 1
                return [list_df1, list_df2], 0, string_des
            else:
                if chip_size and chip_size_hx != chip_size:
                    return '测量时，设置的芯片尺寸与工单不一致！', None, None
                elif Wire_diameter and Wire_diameter_hx not in Wire_diameter:
                    return '测量时，设置的金线线径与工单不一致！', None, None
                else:
                    return '测量时，未设置的金线线径与芯片尺寸！', None, None

        elif string_des == '焊线拉力':
            Wire_diameter_hx = df_pre.loc[0, 'WireDiameter']
            chip_size_hx = df_pre.loc[0, 'ChipSize']
            if (not chip_size or chip_size_hx == chip_size) and (
                    not Wire_diameter or Wire_diameter_hx in Wire_diameter):
                for i in range(len(df_down)):
                    j = i % 6
                    if j == 0 or j == 1 or j == 2 or j == 3:
                        df_after1.loc[0, f'{"DATA" + str(mark1)}'] = df_down.loc[i, 3]
                        if mark1 == 60 or i == len(df_down) - 3:
                            list_df1.append(pd.concat([df_pre, df_after1], axis=1))
                            df_after1 = pd.DataFrame()
                            mark1 = 0
                        mark1 += 1
                    elif j == 4 or j == 5:
                        df_after2.loc[0, f'{"DATA" + str(mark2)}'] = df_down.loc[i, 3]
                        if mark2 == 60 or i == len(df_down) - 1:
                            list_df2.append(pd.concat([df_pre, df_after2], axis=1))
                            df_after2 = pd.DataFrame()
                            mark2 = 0
                        mark2 += 1
                return [list_df1, list_df2], 0, string_des
            else:
                if chip_size and chip_size_hx != chip_size:
                    return '测量时，设置的芯片尺寸与工单不一致！', None, None
                elif Wire_diameter and Wire_diameter_hx not in Wire_diameter:
                    return '测量时，设置的金线线径与工单不一致！', None, None
                else:
                    return '测量时，未设置的金线线径与芯片尺寸！', None, None

        elif string_des == '焊线拉力-齐纳':
            Wire_diameter_hx = df_pre.loc[0, 'WireDiameter']
            chip_size_hx = df_pre.loc[0, 'ChipSize']
            if (not chip_size or chip_size_hx == chip_size) and (
                    not Wire_diameter or Wire_diameter_hx in Wire_diameter):
                for i in range(len(df_down)):
                    j = i % 8
                    if j == 0 or j == 1 or j == 2 or j == 3:
                        df_after1.loc[0, f'{"DATA" + str(mark1)}'] = df_down.loc[i, 3]
                        if mark1 == 60 or i == len(df_down) - 5:
                            list_df1.append(pd.concat([df_pre, df_after1], axis=1))
                            df_after1 = pd.DataFrame()
                            mark1 = 0
                        mark1 += 1
                    elif j == 4 or j == 5:
                        df_after2.loc[0, f'{"DATA" + str(mark2)}'] = df_down.loc[i, 3]
                        if mark2 == 60 or i == len(df_down) - 3:
                            list_df2.append(pd.concat([df_pre, df_after2], axis=1))
                            df_after2 = pd.DataFrame()
                            mark2 = 0
                        mark2 += 1
                    elif j == 6 or j == 7:
                        df_after3.loc[0, f'{"DATA" + str(mark3)}'] = df_down.loc[i, 3]
                        if mark3 == 60 or i == len(df_down) - 1:
                            list_df3.append(pd.concat([df_pre, df_after3], axis=1))
                            df_after3 = pd.DataFrame()
                            mark3 = 0
                        mark3 += 1
                return [list_df1, list_df2, list_df3], 0, string_des
            else:
                if chip_size and chip_size_hx != chip_size:
                    return '测量时，设置的芯片尺寸与工单不一致！', None, None
                elif Wire_diameter and Wire_diameter_hx not in Wire_diameter:
                    return '测量时，设置的金线线径与工单不一致！', None, None
                else:
                    return '测量时，未设置的金线线径与芯片尺寸！', None, None

        elif string_des == '焊线拉力-倒装':
            Wire_diameter_hx = df_pre.loc[0, 'WireDiameter']
            chip_size_hx = df_pre.loc[0, 'ChipSize']
            if (not chip_size or chip_size_hx == chip_size) and (
                    not Wire_diameter or Wire_diameter_hx in Wire_diameter):
                for i in range(len(df_down)):
                    df_after.loc[0, f'{"DATA" + str(mark)}'] = df_down.loc[i, 3]
                    if mark == 60 or i == len(df_down) - 1:
                        list_df.append(pd.concat([df_pre, df_after], axis=1))
                        df_pre = pd.DataFrame()
                        mark = 0
                    mark += 1
                return [list_df], 0, string_des
            else:
                if chip_size and chip_size_hx != chip_size:
                    return '测量时，设置的芯片尺寸与工单不一致！', None, None
                elif Wire_diameter and Wire_diameter_hx not in Wire_diameter:
                    return '测量时，设置的金线线径与工单不一致！', None, None
                else:
                    return '测量时，未设置的金线线径与芯片尺寸！', None, None

        else:
            return [], -1, '数据与所选类型不对应'


def csv_read_2(path, clas):
    df_pre = pd.DataFrame()
    df_after = pd.DataFrame()
    df_after1 = pd.DataFrame()
    df_after2 = pd.DataFrame()
    df_after3 = pd.DataFrame()
    df_after4 = pd.DataFrame()
    df_after5 = pd.DataFrame()

    list_df = []
    list_df1 = []
    list_df2 = []
    list_df3 = []
    list_df4 = []
    list_df5 = []

    row_num = 0
    mark = 1
    mark1 = 1
    mark2 = 1
    mark3 = 1
    mark4 = 1
    mark5 = 1

    # 计算是否有空行
    f = open(path, encoding='gbk')
    str1 = f.readline()
    f.close()
    start_num = 0 if str1.startswith('设备型号') else 1

    df1 = pd.read_csv(path, encoding='gbk', header=None, usecols=[0, 1, 2])
    index1 = df1[df1[0] == 'Cpk, Cp'].index.values[0]

    df_on = df1.loc[1:index1, :3]
    sql_cols = ['TestDate', 'TestTime', 'TestNo', 'Operator', 'Class', 'MachineID', 'WireDiameter', 'ChipSize',
                'DB_glue',
                'Operator', 'LoadUnit', 'Sensor', 'TestMethod', 'AcceptanceVlaue', 'TestingSpeed', 'ShearHeight']

    # 将上半部分的信息转换威df
    for i in range(len(sql_cols)):
        df_pre.loc[0, sql_cols[i]] = df_on.iloc[i, 1]

    # 读取测试的值，并对列进行命名
    df_down = pd.read_csv(path, encoding='gbk', header=None, usecols=[0, 1, 2, 3, 4], skiprows=index1 + start_num + 1)
    df_down = df_down.reset_index(drop=True)
    number_flag, string_des = get_file_type(clas, df_down, start_num, index1)

    if number_flag < 0:
        list_df = string_des
        return list_df, number_flag, string_des
    elif number_flag > 0:
        list_df = f'{clas}文件,{number_flag}行应为{string_des}'
        return list_df, number_flag, string_des
    else:
        if string_des == '固晶推力':
            for i in range(len(df_down)):
                df_after.loc[0, f'{"DATA" + str(mark)}'] = df_down.loc[i, 3]
                if mark == 60 or i == len(df_down) - 1:
                    list_df.append(pd.concat([df_pre, df_after], axis=1))
                    df_pre = pd.DataFrame()
                    mark = 0
                mark += 1
            return [list_df], 0, string_des
        elif string_des == '焊线推力':
            for i in range(len(df_down)):
                j = i % 6
                if j == 0 or j == 1:
                    df_after1.loc[0, f'{"DATA" + str(mark1)}'] = df_down.loc[i, 3]
                    if mark1 == 60 or i == len(df_down) - 5:
                        list_df1.append(pd.concat([df_pre, df_after1], axis=1))
                        df_after1 = pd.DataFrame()
                        mark1 = 0
                    mark1 += 1
                elif j == 2 or j == 3:
                    df_after2.loc[0, f'{"DATA" + str(mark2)}'] = df_down.loc[i, 3]
                    if mark2 == 60 or i == len(df_down) - 3:
                        list_df2.append(pd.concat([df_pre, df_after2], axis=1))
                        df_after2 = pd.DataFrame()
                        mark2 = 0
                    mark2 += 1
                elif j == 4 or j == 5:
                    df_after3.loc[0, f'{"DATA" + str(mark3)}'] = df_down.loc[i, 3]
                    if mark3 == 60 or i == len(df_down) - 1:
                        list_df3.append(pd.concat([df_pre, df_after3], axis=1))
                        df_after3 = pd.DataFrame()
                        mark3 = 0
                    mark3 += 1
            return [list_df1, list_df2, list_df3], 0, string_des
        elif string_des == '焊线推力-齐纳':
            for i in range(len(df_down)):
                j = i % 10
                if j == 0 or j == 1:
                    df_after1.loc[0, f'{"DATA" + str(mark1)}'] = df_down.loc[i, 3]
                    if mark1 == 60 or i == len(df_down) - 9:
                        list_df1.append(pd.concat([df_pre, df_after1], axis=1))
                        df_after1 = pd.DataFrame()
                        mark1 = 0
                    mark1 += 1
                elif j == 2 or j == 3:
                    df_after2.loc[0, f'{"DATA" + str(mark2)}'] = df_down.loc[i, 3]
                    if mark2 == 60 or i == len(df_down) - 7:
                        list_df2.append(pd.concat([df_pre, df_after2], axis=1))
                        df_after2 = pd.DataFrame()
                        mark2 = 0
                    mark2 += 1
                elif j == 4 or j == 5:
                    df_after3.loc[0, f'{"DATA" + str(mark3)}'] = df_down.loc[i, 3]
                    if mark3 == 60 or i == len(df_down) - 5:
                        list_df3.append(pd.concat([df_pre, df_after3], axis=1))
                        df_after3 = pd.DataFrame()
                        mark3 = 0
                    mark3 += 1
                elif j == 6 or j == 7:
                    df_after4.loc[0, f'{"DATA" + str(mark4)}'] = df_down.loc[i, 3]
                    if mark4 == 60 or i == len(df_down) - 3:
                        list_df4.append(pd.concat([df_pre, df_after4], axis=1))
                        df_after4 = pd.DataFrame()
                        mark4 = 0
                    mark4 += 1
                elif j == 8 or j == 9:
                    df_after5.loc[0, f'{"DATA" + str(mark5)}'] = df_down.loc[i, 3]
                    if mark5 == 60 or i == len(df_down) - 1:
                        list_df5.append(pd.concat([df_pre, df_after5], axis=1))
                        df_after5 = pd.DataFrame()
                        mark5 = 0
                    mark5 += 1
            return [list_df1, list_df2, list_df3, list_df4, list_df5], 0, string_des
        elif string_des == '焊线推力-倒装':
            for i in range(len(df_down)):
                j = i % 10
                if j <= 4:
                    df_after1.loc[0, f'{"DATA" + str(mark1)}'] = df_down.loc[i, 3]
                    if mark1 == 60 or i == len(df_down) - 6:
                        list_df1.append(pd.concat([df_pre, df_after1], axis=1))
                        df_after1 = pd.DataFrame()
                        mark1 = 0
                    mark1 += 1
                elif j >= 5:
                    df_after2.loc[0, f'{"DATA" + str(mark2)}'] = df_down.loc[i, 3]
                    if mark2 == 60 or i == len(df_down) - 1:
                        list_df2.append(pd.concat([df_pre, df_after2], axis=1))
                        df_after2 = pd.DataFrame()
                        mark2 = 0
                    mark2 += 1
            return [list_df1, list_df2], 0, string_des
        elif string_des == '焊线拉力':
            for i in range(len(df_down)):
                j = i % 6
                if j == 0 or j == 1 or j == 2 or j == 3:
                    df_after1.loc[0, f'{"DATA" + str(mark1)}'] = df_down.loc[i, 3]
                    if mark1 == 60 or i == len(df_down) - 3:
                        list_df1.append(pd.concat([df_pre, df_after1], axis=1))
                        df_after1 = pd.DataFrame()
                        mark1 = 0
                    mark1 += 1
                elif j == 4 or j == 5:
                    df_after2.loc[0, f'{"DATA" + str(mark2)}'] = df_down.loc[i, 3]
                    if mark2 == 60 or i == len(df_down) - 1:
                        list_df2.append(pd.concat([df_pre, df_after2], axis=1))
                        df_after2 = pd.DataFrame()
                        mark2 = 0
                    mark2 += 1
            return [list_df1, list_df2], 0, string_des
        elif string_des == '焊线拉力-齐纳':
            for i in range(len(df_down)):
                j = i % 8
                if j == 0 or j == 1 or j == 2 or j == 3:
                    df_after1.loc[0, f'{"DATA" + str(mark1)}'] = df_down.loc[i, 3]
                    if mark1 == 60 or i == len(df_down) - 5:
                        list_df1.append(pd.concat([df_pre, df_after1], axis=1))
                        df_after1 = pd.DataFrame()
                        mark1 = 0
                    mark1 += 1
                elif j == 4 or j == 5:
                    df_after2.loc[0, f'{"DATA" + str(mark2)}'] = df_down.loc[i, 3]
                    if mark2 == 60 or i == len(df_down) - 3:
                        list_df2.append(pd.concat([df_pre, df_after2], axis=1))
                        df_after2 = pd.DataFrame()
                        mark2 = 0
                    mark2 += 1
                elif j == 6 or j == 7:
                    df_after3.loc[0, f'{"DATA" + str(mark3)}'] = df_down.loc[i, 3]
                    if mark3 == 60 or i == len(df_down) - 1:
                        list_df3.append(pd.concat([df_pre, df_after3], axis=1))
                        df_after3 = pd.DataFrame()
                        mark3 = 0
                    mark3 += 1
            return [list_df1, list_df2, list_df3], 0, string_des
        elif string_des == '焊线拉力-倒装':
            for i in range(len(df_down)):
                df_after.loc[0, f'{"DATA" + str(mark)}'] = df_down.loc[i, 3]
                if mark == 60 or i == len(df_down) - 1:
                    list_df.append(pd.concat([df_pre, df_after], axis=1))
                    df_pre = pd.DataFrame()
                    mark = 0
                mark += 1
            return [list_df], 0, string_des
        else:
            return [], -1, '数据与所选类型不对应'


def get_file_type(clas, df, start_num, index):
    """
    根据df的性质状态去判断是否为正常、齐纳、倒装
    :param clas:
    :param df:
    :return:
    """
    if clas == '固晶推力':
        for i in range(len(df)):
            if df.iloc[i, 2] != 'Chip_shear':
                row_num = index + i + start_num + 2
                return row_num, 'Chip_shear'
        return 0, '固晶推力'
    elif clas == '焊线拉力':
        # 倒装拉力
        if len(set(list(df[2]))) == 1 and 'B_open_chip_to_chip' in set(list(df[2])):
            for i in range(len(df)):
                if df.iloc[i, 2] != 'B_open_chip_to_chip':
                    row_num = index + i + start_num + 2
                    return row_num, 'B_open_chip_to_chip'
            return 0, '焊线拉力-倒装'
        elif len(set(list(df[2]))) == 2 and 'B_open_chip_to_chip' in set(
                list(df[2])) and 'B_open_chip_to_bonding' in set(list(df[2])):
            if (len(df) <= 8 and len(df) % 6 == 0) or (
                    len(df) > 8 and len(df) % 6 == 0 and df.iloc[11, 2] == 'B_open_chip_to_bonding'):
                for i in range(len(df)):
                    j = i % 6
                    if j <= 3 and df.loc[i, 2] != 'B_open_chip_to_chip':
                        row_num = index + i + start_num + 2
                        return row_num, 'B_open_chip_to_chip'
                    elif j >= 4 and df.loc[i, 2] != 'B_open_chip_to_bonding':
                        row_num = index + i + start_num + 2
                        return row_num, 'B_open_chip_to_bonding'
                return 0, '焊线拉力'
            elif (len(df) <= 8 and len(df) % 8 == 0) or (
                    len(df) > 8 and len(df) % 8 == 0 and df.iloc[11, 2] == 'B_open_chip_to_chip'):
                for i in range(len(df)):
                    j = i % 8
                    if (j <= 3 or j >= 6) and df.loc[
                        i, 2] != 'B_open_chip_to_chip':
                        row_num = index + i + start_num + 2
                        return row_num, 'B_open_chip_to_chip'
                    elif (j == 4 or j == 5) and df.loc[i, 2] != 'B_open_chip_to_bonding':
                        row_num = index + i + start_num + 2
                        return row_num, 'B_open_chip_to_bonding'
                return 0, '焊线拉力-齐纳'
            else:
                return -1, '数据类型选择错误'
    elif clas == '焊线推力':
        if len(set(list(df[2]))) == 2 and 'P_shear' in set(list(df[2])) and 'N_shear' in set(list(df[2])) and len(
                df) % 10 == 0:
            for i in range(len(df)):
                j = i % 10
                if j <= 4 and df.loc[i, 2] != 'P_shear':
                    row_num = index + i + start_num + 2
                    return row_num, 'P_shear'
                elif j > 4 and df.loc[i, 2] != 'N_shear':
                    row_num = index + i + start_num + 2
                    return row_num, 'N_shear'
            return 0, '焊线推力-倒装'
        elif len(set(list(df[2]))) == 3 and 'P_shear' in set(list(df[2])) and 'N_shear' in set(
                list(df[2])) and 'Bonding_shear' in set(list(df[2])) and len(df) % 6 == 0:
            if (len(df) <= 10 and len(df) % 6 == 0) or (
                    len(df) > 10 and len(df) % 6 == 0 and df.iloc[11, 2] == 'Bonding_shear'):
                for i in range(len(df)):
                    j = i % 6
                    if j <= 1 and df.loc[i, 2] != 'P_shear':
                        row_num = index + i + start_num + 2
                        return row_num, 'P_shear'
                    elif (j == 2 or j == 3) and df.loc[i, 2] != 'N_shear':
                        row_num = index + i + start_num + 2
                        return row_num, 'N_shear'
                    elif j > 3 and df.loc[i, 2] != 'Bonding_shear':
                        row_num = index + i + start_num + 2
                        return row_num, 'Bonding_shear'
                return 0, '焊线推力'
            elif (len(df) <= 10 and len(df) % 10 == 0) or (
                    len(df) > 10 and len(df) % 10 == 0 and df.iloc[11, 2] == 'P_shear'):
                for i in range(len(df)):
                    j = i % 10
                    if (j <= 1 or j == 6 or j == 7) and df.loc[i, 2] != 'P_shear':
                        row_num = index + i + start_num + 2
                        return row_num, 'P_shear'
                    elif (j == 2 or j == 3 or j == 8 or j == 9) and df.loc[i, 2] != 'N_shear':
                        row_num = index + i + start_num + 2
                        return row_num, 'N_shear'
                    elif (j == 4 or j == 5) and df.loc[i, 2] != 'Bonding_shear':
                        row_num = index + i + start_num + 2
                        return row_num, 'Bonding_shear'
                return 0, '焊线推力-齐纳'
        else:
            return -1, '数据类型选择错误'
    else:
        return -1, '数据类型选择错误'

if __name__ == '__main__':
    path = r'E:\QQ文档\1402653174\FileRecv\CTQ数据\固晶\Result_5102-20210313_001.csv'
    csv_read(path, '固晶推力', )
